﻿using System;
using System.Configuration;
using System.Linq;

namespace FBGraph.Configuration
{
    /// <summary>Encapsulated functionality for reading information out of the configuration file.</summary>
    public class FBGraphConfigurationManager
    {
        internal String Environment { get { return ConfigurationManager.AppSettings["Environment"]; } }

        internal Int64? OverriddenClientID { get { return ConfigurationManager.AppSettings["ClientID"] == null ? (Int64?)null : Int64.Parse(ConfigurationManager.AppSettings["ClientID"]); } }
        internal String OverriddenClientSecret { get { return ConfigurationManager.AppSettings["ClientSecret"]; } }

        internal FBGraphApplicationElement GetCurrentApplication()
        {
            var api = GetApiConfiguration();
            if (api == null) return null;
            else
            {
                var nonEnvApp = api.Applications.SingleOrDefault(a => String.IsNullOrEmpty(a.Environment));
                if (Environment == null) return nonEnvApp;
                else return api.Applications.SingleOrDefault(a => a.Environment == Environment) ?? nonEnvApp;
            }
        }

        /// <summary>Gets a <see cref="Boolean" /> value representing whether the configuration has an application configured.</summary>
        public Boolean HasConfiguredApplication
        {
            get
            {
                return (OverriddenClientID.HasValue && !String.IsNullOrEmpty(OverriddenClientSecret)) ||
                    GetCurrentApplication() != null;
            }
        }

        /// <summary>Gets the active application's Client ID.</summary>
        public Int64 ClientID
        {
            get
            {
                if (OverriddenClientID.HasValue && !String.IsNullOrEmpty(OverriddenClientSecret)) return OverriddenClientID.Value;
                else
                {
                    var app = GetCurrentApplication();
                    if (app == null) throw new ApplicationException("No valid credentials have been configured. Please check your web.config or app.config file.");
                    else return app.ClientID;
                }
            }
        }

        /// <summary>Gets the active application's Client Secret.</summary>
        public String ClientSecret
        {
            get
            {
                if (OverriddenClientID.HasValue && !String.IsNullOrEmpty(OverriddenClientSecret)) return OverriddenClientSecret;
                else
                {
                    var app = GetCurrentApplication();
                    if (app == null) throw new ApplicationException("No valid credentials have been configured. Please check your web.config or app.config file.");
                    else return app.ClientSecret;
                }
            }
        }

        /// <summary>Contains configuration elements specific to the FBGraph API.</summary>
        public FBGraphApiSection GetApiConfiguration()
        {
            var apiSection = ConfigurationManager.GetSection("fbgraph/api");
            return (FBGraphApiSection)apiSection;
        }
    }
}
